<?php
header("Content-type: text/html; charset=utf-8");
$appid="wxb141e53a6d759dd5";
$secret="7d924dd63699147fcc05819ab27ecd07";
$gotourl=$_GET['gotourl'];
$state=$_GET['state'];

$selfurl="http://".$_SERVER ['HTTP_HOST'].$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
$snsapi_base_url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.urlencode($selfurl).'&response_type=code&scope=snsapi_base&state='.$state.'#wechat_redirect';
$snsapi_userinfo_url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.urlencode($selfurl).'&response_type=code&scope=snsapi_userinfo&state='.$state.'#wechat_redirect';
//ment.location.href='https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appid+'&redirect_uri=http%3a%2f%2fwap.relonline.cn%2f2015_Fobenshidao%2foauth2.php&response_type=code&scope=snsapi_userinfo&state='+appname+'#wechat_redirect';

    if (isset($_GET['code']))
    {
        //echo $_GET['code'];
        //获取access_token
        $code=$_GET['code'];
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code";
        $output = https_request($url);//请求数据
        $access_token_obj = json_decode($output);
        //echo($access_token_obj->access_token);
        $access_token=$access_token_obj->access_token;//网页授权接口调用凭证
        $openid=$access_token_obj->openid;//用户唯一标识
        $refresh_token=$access_token_obj->refresh_token;//用户刷新access_token


        if($access_token){
            //读取用户信息
            $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
            $output = https_request($url);
            $userinfo_obj = json_decode($output);
            $openid=$userinfo_obj->openid;//openid
            $openid_md5=md5_str($openid,'woshitianchai&2015');
            $nickname = $userinfo_obj->nickname;//昵称
            $sex=$userinfo_obj->sex;//性别
            $province=$userinfo_obj->province;//省份
            $city=$userinfo_obj->city;//城市
            $country=$userinfo_obj->country;//国家
            $headimgurl=$userinfo_obj->headimgurl;//头像图片
            if($openid){
                //获取用户信息成功，存储用户信息到cookies中，并跳转到首页
                setcookie($state."_access_token", $access_token, time()+3600*24);
                setcookie($state."_openid", $openid, time()+3600*24);
                setcookie($state."_openid_md5", $openid_md5, time()+3600*24);
                setcookie($state."_nickname", urlencode($nickname), time()+3600*24);
                setcookie($state."_sex", urlencode($sex), time()+3600*24);
                setcookie($state."_province", urlencode($province), time()+3600*24);
                setcookie($state."_city", urlencode($city), time()+3600*24);
                setcookie($state."_country", urlencode($country), time()+3600*24);
                setcookie($state."_headimgurl", urlencode($headimgurl), time()+3600*24);

                $jump_url=$gotourl."#openid=".$openid."&openid_md5=".$openid_md5."&nickname=".urlencode(urlencode($nickname));

               //echo $state;
                //echo ("跳转地址=".$jump_url);
                echo "<script language='javascript' type='text/javascript'>";
                echo "window.location.href='$jump_url'";
                echo "</script>";
            }else{
                //echo "NO userinfo";
                //echo '显示授权';
                echo "<script language='javascript' type='text/javascript'>";
                echo "window.location.href='$snsapi_userinfo_url';";
                echo "</script>";
            }
        }
    }else{
        //echo "NO code";
        //echo ("静默授权地址=".$snsapi_base_url);
        echo "<script language='javascript' type='text/javascript'>";
        echo "window.location.href='$snsapi_base_url';";
        echo "</script>";
    }

//用于微信接口数据传输
function https_request($url, $data = null)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}
//md5加密
function md5_str($data, $append)
{
    return md5($data.$append);
}
?>